package com.essg.sdc.study;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ControlBreak {

	/*
	 * Test SVN
	 */
	private List<EmpData> mEmpData;
	private int mCurrent;
	public static final String HIGH_VALUE = String.valueOf(0xFF);

	private EmpData mSvEmp;
	private EmpData mTrEmp;

	private boolean mHeader;

	public ControlBreak(){
		mEmpData = new ArrayList<ControlBreak.EmpData>();
		StoreEmp();
		CollectionSort();
		mCurrent = 0;
	}

	public void ListEmp(){

		mSvEmp = new EmpData();
		mTrEmp = nextEmp();
		mHeader = false;

		while(!mTrEmp.getDivcd().equals(HIGH_VALUE)){
			while(mSvEmp.getDivcd().equals(mTrEmp.getDivcd())){
				printDetail();
				mTrEmp = nextEmp();
			}
			mSvEmp.setDivcd(mTrEmp.getDivcd());
			printDivision();
		}

	}

	private void printDivision(){
		if(mSvEmp.getDivcd().equals(HIGH_VALUE)) return;

		System.out.println("Div Sec Esscd   Name            Pay");
		System.out.print(mSvEmp.getDivcd() + " ");
		mHeader = true;
	}

	private void printDetail(){

		String _detail;

		if(mHeader){
			_detail = mTrEmp.getSeccd() + "  " + mTrEmp.getEsscd() + " " + mTrEmp.getName() + String.format(" \\%1$0,3d", mTrEmp.getPay());
			mHeader = false;
		}else{
			_detail = "    " + mTrEmp.getSeccd() + "  " + mTrEmp.getEsscd() + " " + mTrEmp.getName() + String.format(" \\%1$0,3d", mTrEmp.getPay());
		}
		System.out.println(_detail);
	}

	final protected void StoreEmp(){

		for(int i=1; i<51; i++){
			String _esscd = String.format("ess%1$04d", i);
			String _devcd = "000";
			String _seccd = "00";

			switch (i%5) {
			case 0:
				_devcd = String.format("%1$03d", 5);
				break;
			default:
				_devcd = String.format("%1$03d", i%5);
				break;
			}

			switch (i%3) {
			case 0:
				_seccd = String.format("%1$02d", 3);
				break;
			default:
				_seccd = String.format("%1$02d", i%3);
				break;
			}

			String _name = "essEmployee(" + String.format("%1$02d",i) +")";
			int _pay = (i*1000)+200000;

			EmpData _data = new EmpData();
			_data.setEsscd(_esscd);
			_data.setDivcd(_devcd);
			_data.setSeccd(_seccd);
			_data.setName(_name);
			_data.setPay(_pay);

			mEmpData.add(_data);
		}

	}

	final protected void CollectionSort(){

		Collections.sort(mEmpData, new Comparator<EmpData>() {
			@Override
			public int compare(EmpData o1, EmpData o2) {

				String _o1 = o1.getDivcd() + o1.getSeccd();
				String _o2 = o2.getDivcd() + o2.getSeccd();
				return (_o1).compareTo(_o2);
			}
		});

	}

	final protected EmpData nextEmp(){
		mCurrent += 1;
		if(mCurrent > mEmpData.size()){
			EmpData _result = new EmpData();
			_result.setDivcd(HIGH_VALUE);
			_result.setSeccd(HIGH_VALUE);
			return _result;
		}
		return mEmpData.get(mCurrent-1);
	}

	public static void main(String[] args) {

		ControlBreak _obj = new ControlBreak();
		_obj.ListEmp();

	}

	class EmpData{

		String esscd;
		String divcd;
		String seccd;
		String name;
		int pay;

		EmpData(){
			esscd = "";
			divcd = "";
			seccd = "";
			name = "";
			pay = 0;
		}

		public String getEsscd() {
			return esscd;
		}
		public void setEsscd(String esscd) {
			this.esscd = esscd;
		}
		public String getDivcd() {
			return divcd;
		}
		public void setDivcd(String divcd) {
			this.divcd = divcd;
		}
		public String getSeccd() {
			return seccd;
		}
		public void setSeccd(String seccd) {
			this.seccd = seccd;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getPay() {
			return pay;
		}
		public void setPay(int pay) {
			this.pay = pay;
		}

	}

}
